package view;

import main.WinkelApplication;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import model.Product;

public class ProductDetails extends JPanel implements MouseListener, ActionListener {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    private final int product_id;

    public ProductDetails(int product_id) {
        super();
        /** Set name, layout and Background */
        this.setLayout(null);
        this.product_id = product_id;
        initComponents();
    }

    private void initComponents() {
        addTitle(dim.width, dim.height);        
        addProduct();
        addBasket();
        addBack();
    }

    private void addTitle(int width, int heigth) {
        JLabel lblTitle1 = new JLabel("Bekijk product",SwingConstants.CENTER);
        lblTitle1.setBounds(20, 20, (int)(width * 0.76), 40);
        lblTitle1.setFont(WinkelApplication.computeFontSize(1));
        lblTitle1.setName("-1");
        add(lblTitle1);
    }
    
    public void addBack() {
        ClassLoader cldr = this.getClass().getClassLoader();
        URL imageURL = cldr.getResource("view/terug.png");
        ImageIcon icon = new ImageIcon(imageURL);
        JLabel back = new JLabel(icon);
        back.setName("a");
        back.setBounds(dim.width -((dim.width/5) + 82), 40, 48, 48);
        back.addMouseListener(this);
        add(back);
    }

    private void addProduct() {
        Product currentProduct = WinkelApplication.getQueryManager().getProduct(product_id);
        
        JLabel lblProductName = new JLabel();
        lblProductName.setText(currentProduct.getName());
        lblProductName.setBounds(20, 60, 500, 20);
        lblProductName.setFont(WinkelApplication.FONT_16_BOLD);
        add(lblProductName);

        JLabel lblDescription = new JLabel();
        lblDescription.setText(currentProduct.getDescription());
        lblDescription.setBounds(20, 80, 500, 20);
        lblDescription.setFont(WinkelApplication.FONT_10_PLAIN);
        add(lblDescription);

        JLabel lblProductPrice = new JLabel();
        lblProductPrice.setText("Prijs: " + WinkelApplication.CURRENCY + 
                WinkelApplication.PRICE_FORMAT.format(currentProduct.getPrice()));
        lblProductPrice.setBounds(20, 100, 500, 20);
        lblProductPrice.setFont(WinkelApplication.FONT_10_PLAIN);
        add(lblProductPrice);

        JButton btnOrder = new JButton("Toevoegen aan Winkelmand");
        btnOrder.setName("2");
        btnOrder.setBounds(20, 140, 230, 20);
        btnOrder.setFont(WinkelApplication.FONT_12_BOLD);
        btnOrder.addActionListener(this);
        add(btnOrder);
    }

    private void addBasket() {
        BasketDisplay basket = new BasketDisplay();
        add(basket);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        // product wordt toegevoegd aan de winkelmand
        Product currentProduct = WinkelApplication.getQueryManager().getProduct(product_id);
        WinkelApplication.getBasket().addProduct(currentProduct);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        // the user clicked on the back button, go back to the last category
        ProductList productList = new ProductList(WinkelApplication.getInstance().getLastCategory());
        WinkelApplication.getInstance().showPanel(productList);
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        // Intentionally left blank.
    }
}
